import path from "path";
import { UnifiedWebpackPluginV5 } from "weapp-tailwindcss/webpack";

const env = process.env.NODE_ENV === "development" ? "dev" : "prd";

const config = {
  projectName: "d8d-mobile-mini-player",
  date: "2024-6-19",
  designWidth: 375,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
    375: 2 / 1,
  },
  sourceRoot: "src",
  // outputRoot: "dist",
  outputRoot: `target/${process.env.TARO_ENV}/${env}`,
  plugins: ["@tarojs/plugin-html"],
  defineConstants: {},
  copy: {
    patterns: [
      // {
      //   from: "src/appCode.js",
      //   to: `target/${process.env.TARO_ENV}/${env}/appCode.js`,
      // },
    ],
    options: {},
  },
  framework: "react",
  compiler: {
    type: "webpack5",
    prebundle: { enable: false },
  },
  mini: {
    webpackChain(chain, webpack) {
      chain.merge({
        externals: {
          // "./../appCode": `require('./../appCode.js')`, // 将 appcode.js 指定为外部依赖
          // "./appCode": `require('./appCode.js')`, // 将 appcode.js 指定为外部依赖
          // "./appConfig": `require('./appConfig.js')`, // 将 appConfig.js 指定为外部依赖
        },
        plugin: {
          install: {
            plugin: UnifiedWebpackPluginV5,
            args: [
              {
                appType: "taro",
              },
            ],
          },
        },
      });
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          selectorBlackList: ["nut-"],
        },
      },
      url: {
        enable: true,
        config: {
          limit: 1024, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: "module", // 转换模式，取值为 global/module
          generateScopedName: "[name]__[local]___[hash:base64:5]",
        },
      },
    },
    // debugReact: true,
  },
  h5: {
    publicPath: "/",
    staticDirectory: "static",
    // esnextModules: ['nutui-react'],
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          selectorBlackList: ["nut-"],
        },
      },
      autoprefixer: {
        enable: true,
        config: {},
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: "module", // 转换模式，取值为 global/module
          generateScopedName: "[name]__[local]___[hash:base64:5]",
        },
      },
    },
  },
  alias: {
    "@": path.resolve(__dirname, "..", "src"),
    // "d8d-mobile-ui": path.resolve(__dirname, "..", "src/package/index.ts"),
  },
};

module.exports = function (merge) {
  if (process.env.NODE_ENV === "development") {
    return merge({}, config, require("./dev"));
  }
  return merge({}, config, require("./prod"));
};
